package com.koron.system.config;

import com.koron.common.core.interceptor.FieldInterceptor;
import lombok.AllArgsConstructor;
import org.apache.ibatis.plugin.Interceptor;
import org.koron.ebs.mybatis.ADOSessionImpl;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @author jzp
 * @since 2021/7/13 10:13
 */
@Configuration
@AllArgsConstructor
public class DataSourceConfig {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource primaryDataSource() {
        DataSource ds = DataSourceBuilder.create().build();
        ADOSessionImpl session = new ADOSessionImpl();
        session.removeInterceptor("_default", "com.github.pagehelper.PageInterceptor");
        session.addInterceptor("_default", "com.github.pagehelper.PageInterceptor");
        //开启/关闭-自动转换驼峰标识
        SessionFactory.setMapUnderscoreToCamelCase(true);
        session.registeDataSourceMap("_default", ds);
        return ds;
    }

    @Bean
    public Interceptor addInterceptor() {
        return new FieldInterceptor();
    }

}